package com.kinon.tyjaccount.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.widget.Toast;

import com.google.gson.Gson;
import com.kinon.tyjaccount.sqlitebean.BaseCard;
import com.kinon.tyjaccount.sqlitebean.BaseOrder;
import com.kinon.tyjaccount.sqlitebean.Card;
import com.kinon.tyjaccount.sqlitebean.OrderBean;
import com.kinon.tyjaccount.sqlitebean.OrderDetail;
import com.kinon.tyjaccount.tools.HttpTools;
import com.kinon.tyjaccount.tools.NetWorkUtil;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONObject;
import org.litepal.crud.DataSupport;

import java.util.ArrayList;

/**
 * Created by luohao on 2017-07-31.
 */

public class GetMakemealService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        getOrder();
        getCard();
        return super.onStartCommand(intent, flags, startId);
    }

    //获取卡号
    public void getCard(){
        HttpTools httpTools=new HttpTools(getApplicationContext());
        if(NetWorkUtil.isNetwork(getApplicationContext())==false)
        {
            Toast.makeText(getApplicationContext(),"网络不可用",Toast.LENGTH_SHORT).show();
            return;
        }
        SharedPreferences sp = getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        String token = sp.getString("data", "");
        httpTools.getCards(token,new JsonGetCard());
    }

    private  class  JsonGetCard extends JsonHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            super.onSuccess(statusCode, headers, response);
//            Log.e("lh", "getcard== "+response.toString());
            try {
                boolean code= response.getBoolean("code");
                if(code==true) {
                    Gson gson=new Gson();
                    BaseCard data=gson.fromJson(response.toString(),BaseCard.class);
                    DataSupport.deleteAll(Card.class);
                    DataSupport.saveAll(data.getData());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, String responseBody, Throwable e) {
            super.onFailure(statusCode, headers, responseBody, e);
//            Log.e("lh", "getcard== "+responseBody );
        }
    }

    /**获得订单*/
    private void getOrder(){
        HttpTools httpTools=new HttpTools(getApplicationContext());
        if(NetWorkUtil.isNetwork(getApplicationContext())==false)
        {
            Toast.makeText(getApplicationContext(),"网络不可用",Toast.LENGTH_SHORT).show();
            return;
        }
        SharedPreferences sp = getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        String token = sp.getString("data", "");
        httpTools.backupgetOrders(token,new JsonGetOrder());

    }

    private  class  JsonGetOrder extends JsonHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            super.onSuccess(statusCode, headers, response);
            try {
                boolean code= response.getBoolean("code");
                if(code==true){
                    //删除数据库表中的所有数据
                    DataSupport.deleteAll(OrderBean.class);
                    Gson gson=new Gson();
                    BaseOrder order=gson.fromJson(response.toString(),BaseOrder.class);
                    ArrayList<OrderBean> list=order.getData();
                    for (OrderBean o:list){
                        o.save();
                        for(OrderDetail d:o.getDetails()){
                            d.save();
                        }
                    }
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, String responseBody, Throwable e) {
            super.onFailure(statusCode, headers, responseBody, e);
        }
    }
}
